package org.example.struct.array;

import com.alibaba.fastjson.JSON;

/**
 * @author liyishan
 * @date 2023/8/3 09:19
 * @apiNote
 */

public class Test00566MatrixReshape {
    public static void main(String[] args) {
        int[][] mat = {{1,2},{3,4}};
        int[][] res= test(mat,2,2);
        System.out.println(JSON.toJSON(res));
    }

    public static int[][] test(int[][] mat, int r, int c) {
        int m = mat.length;
        int n = mat[0].length;
        if (m * n != r * c) {
            return mat;
        }
        int[] temp = new int[mat.length * mat[0].length];
        int[][] res = new int[r][c];
        int len = 0;
        for(int i = 0; i<mat.length;i++){
            for(int j = 0; j<mat[0].length;j++){
                temp[len++] = mat[i][j];
            }
        }
        int size = 0;
        for(int i=0;i<r;i++){
            for(int j =0;j<c;j++){
                res[i][j] = temp[size ++];
            }
        }
        return res;
    }


    /**
     * 针对列进行处理
     * @param mat
     * @param r
     * @param c
     * @return
     */
    public static int[][] test1(int[][] mat, int r, int c) {
        int m = mat.length;
        int n = mat[0].length;
        if (m * n != r * c) {
            return mat;
        }
        int[][] res = new int[r][c];
        for(int i=0;i<m*n;i++){
            res[i/c][i%c] = mat[i/n][i%n];
        }
        return res;
    }
}
